package com.yvan.game.merge.ui;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.yvan.game.merge.RouteProperties;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UiController {

    @Autowired
    private RouteProperties routeProperties;

    @Value("${route.address}")
    private String address;

    @RequestMapping("/**/*.html")
    public ModelAndView ftl(HttpServletRequest request) {
        val paths = parsePath(request);
        val ftl = Joiner.on("/").join(paths) + ".ftl";
        return new ModelAndView(ftl);
    }

    private List<String> parsePath(HttpServletRequest request) {
        String uri = request.getRequestURI();
        return Splitter.on("/").omitEmptyStrings().splitToList(uri);
    }
}
